<?php

namespace App\Http\Controllers\Center;

use App\Http\Controllers\Controller;
use App\Http\Traits\Auth\SocialiteTrait;
use App\Models\UserBinds;

class BindsController extends Controller
{
    use SocialiteTrait;

    /**
     * 显示账号绑定页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showBinds()
    {
        $binds = $this->user()->binds->keyBy('type');

        return $this->view('center.binds', ['binds' => $binds]);
    }

    /**
     * 账号绑定: 跳转
     *
     * @param $type
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function bind($type)
    {
        return $this->socialite($type)->redirect();
    }

    /**
     * 解除账号绑定
     *
     * @param $type
     *
     * @return $this|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function unbind($type)
    {
        if ($deletedRows = UserBinds::where('type', $type)->delete()) {

            return $this->back()
                ->withSuccess('解除绑定成功');
        }

        return $this->back()
            ->withError('账号绑定记录不存在');
    }

}